// pages/home/productOrder/productOrder.js
const app = getApp()
const request = require("../../../utils/network.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgBase: app.imgBase,
    product: {},
    region: ['浙江省', '杭州市', '拱墅区'],
    customItem: '全部',
    typeId: "",
    buyNum: 0,
    offPrice: 0, /* 优惠减免 */
    transportPrice: 0,
    totalNum: 0,
    totalPrice: 30,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getProductInfo(options.typeId);
    this.setData({
      typeId: options.typeId,
      buyNum: options.number,
      totalNum: options.number
    })
  },
  getProductInfo(id) {
    var that = this;
    var url = '/goodsStyle/getGoodsByStyleId/'+id;
    request.request(url, { id: id }, function (res) {  
      that.setData({
        product: res.data,
        totalPrice: Number(res.data.goodsStyle.price) * Number(that.data.buyNum)
      });
    }, function () {

    }, 'GET');
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindRegionChange(e) {
  
    this.setData({
      region: e.detail.value
    })
  },
  minuNum() {
    if (this.data.buyNum > 1) {
      this.setData({
        buyNum: Number(this.data.buyNum) - 1,
      });
    }
    this.setData({
      totalNum: this.data.buyNum,
      totalPrice: Number(this.data.buyNum) * Number(this.data.product.goodsStyle.price) - Number(this.data.offPrice) + Number(this.data.transportPrice)
    });
  },
  plusNum() {
    if (this.data.buyNum) {
      this.setData({
        buyNum: Number(this.data.buyNum) + 1
      });
    }
    this.setData({
      totalNum: this.data.buyNum,
      totalPrice: Number(this.data.buyNum) * Number(this.data.product.goodsStyle.price) - Number(this.data.offPrice) + Number(this.data.transportPrice)
    });
  },
  quickBuy(){
    let url = "";
    wx.redirectTo({
      url:url
    })
  }
})